package com.borqsprocessmanager.db;

import java.io.File;

import com.borqsprocessmanager.App;
import com.borqsprocessmanager.db.DBProvider.ActionTable;
import com.borqsprocessmanager.db.DBProvider.NetWorkTable;
import com.borqsprocessmanager.db.DBProvider.ProcessTable;
import com.borqsprocessmanager.db.DBProvider.SamplesTable;
import com.borqsprocessmanager.db.DBProvider.WhiteListTable;
import com.borqsprocessmanager.util.FileUtil;
import com.borqsprocessmanager.util.PreferencesUtil;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
    private static final String TAG = "DBHelper";

    private static final String samples_table = "create table "
            + SamplesTable.TABLE_NAME + "(" + SamplesTable._ID
            + " integer primary key autoincrement, " + SamplesTable.START_TIME
            + " integer, " + SamplesTable.END_TIME + " integer, "
            + SamplesTable.START_LEVEL + " integer, " + SamplesTable.END_LEVEL
            + " integer)";

    private static final String process_table = "create table "
            + ProcessTable.TABLE_NAME + "(" + ProcessTable._ID
            + " integer primary key autoincrement, " + ProcessTable.IS_SYSTEM
            + " integer, " + ProcessTable.PID + " integer, "
            + ProcessTable.TYPE + " integer, " + ProcessTable.PROCESS_NAME
            + " text, " + ProcessTable.RATIO + " real, "
            + ProcessTable.SAMPLE_ID + " integer, " + ProcessTable.UID
            + " integer, " + ProcessTable.PKG_NAME + " text, "
            + ProcessTable.APPNAME_NAME + " text, " + ProcessTable.RX_BYTES
            + " integer, " + ProcessTable.TX_BYTES + " integer, "
            + ProcessTable.TOTAL_BYTES + " integer)";

    private static final String white_table = "create table "
            + WhiteListTable.TABLE_NAME + "(" + WhiteListTable._ID
            + " integer primary key autoincrement, " + WhiteListTable.IS_SYSTEM
            + " integer, " + WhiteListTable.PID + " integer, "
            + WhiteListTable.TYPE + " integer, " + WhiteListTable.PROCESS_NAME
            + " text, " + WhiteListTable.RATIO + " real, "
            + WhiteListTable.SAMPLE_ID + " integer, " + WhiteListTable.UID
            + " integer, " + WhiteListTable.PKG_NAME + " text, "
            + WhiteListTable.APP_NAME + " text, " + WhiteListTable.RX_BYTES
            + " integer, " + WhiteListTable.TX_BYTES + " integer, "
            + WhiteListTable.TOTAL_BYTES + " integer)";

    private static final String action_table = "create table "
            + ActionTable.TABLE_NAME + "(" + ActionTable._ID
            + " integer primary key autoincrement, " + ActionTable.ACATION_TYPE
            + " integer, " + ActionTable.PKG_NAME + " text, "
            + ActionTable.PROCESS_NAME + " text, " + ActionTable.APPNAME
            + " text)";

    private static final String network_table = "create table"
            + NetWorkTable.TABLE_NAME + "(" + NetWorkTable._ID
            + " integer primary key autoincrement, " + NetWorkTable.ICON
            + " integer, " + NetWorkTable.UID + " integer, "
            + NetWorkTable.PKG_NAME + " text, " + NetWorkTable.APPNAME_NAME
            + " text, " + NetWorkTable.RX_BYTES + " integer, "
            + NetWorkTable.TX_BYTES + " integer, " + NetWorkTable.TOTAL_BYTES
            + " integer, " + NetWorkTable.SAMLPE_ID + " integer)";

    private static final String wakelock_table = "create table"
            + " wakelock_samples(id integer primary key autoincrement,"
            + " tag txt," + " time_length integer," + " uid integer,"
            + " sample_id integer)";

    private static final String DEVELOP_TABLE = "create table"
            + " developer_data(_id integer primary key autoincrement,"
            + " operation text," + " last_time integer," + " time integer,"
            + " sample_id integer," + " wifi_state integer,"
            + " gps_state integer," + " sync_state integer,"
            + " airplane_state integer," + " gprs_state integer,"
            + " is_process integer," + " start_average_v integer)";

    private static final String ESTIMEATE_TABLE = "create table"
            + " process(_id integer primary key autoincrement,"
            + " samlpe_id integer," + " uid integer," + " pkg_name text,"
            + " rx_bytes integer," + " tx_bytes integer,"
            + " cpu_percent integer," + " start_time integer,"
            + " stop_time integer)";

    private static DBHelper instance = null;
    SQLiteDatabase db;

    public static DBHelper getInstance(Context context) {
        if (null == instance) {
            instance = new DBHelper(context);
        }
        return instance;
    }

    public static void newInstance(Context context) {
        instance = new DBHelper(context);
    }

    public DBHelper(Context context) {
        super(context, DBProvider.DB_NAME, null, 4);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        this.db = db;
        db.execSQL(samples_table);
        db.execSQL(process_table);
        db.execSQL(action_table);
        db.execSQL(white_table);

        Log.d(TAG, "create tables ok");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        com.borqsprocessmanager.util.Log.v(TAG, "onUpgrade_oldVersion==="
                + oldVersion);
        com.borqsprocessmanager.util.Log.v(TAG, "onUpgrade_newVersion==="
                + newVersion);

        switch (oldVersion) {
        case 1:

        case 2:
            db.delete(samples_table, null, null);
            db.delete(action_table, null, null);
            db.delete(process_table, null, null);
            db.delete(white_table, null, null);

            db.execSQL(samples_table);
            db.execSQL(process_table);
            db.execSQL(action_table);
            db.execSQL(white_table);
        case 3:
            File fileDir = App.mContext.getFilesDir();
            FileUtil.delete(fileDir);
        default:
            break;
        }
    }
}
